﻿using System;
using System.Configuration;
using UDS.Entity;

namespace UDS.Configuration
{
    /// <summary>
    /// Contains the config of the UDS application
    /// </summary>
    public class UDSSection : ConfigurationSection
    {
        private static readonly ConfigurationElementProperty s_elemProp;

        static UDSSection()
        {
            // Initialize static properties
            s_elemProp = new ConfigurationElementProperty(new CallbackValidator(typeof(UDSSection), ValidateClassType));
        }

        [ConfigurationProperty("classType", DefaultValue = ClassType.Unknown, IsRequired = false)]
        public ClassType ClassType
        {
            get { return (ClassType)this["classType"]; }
            set { this["classType"] = value; }
        }

        protected override ConfigurationElementProperty ElementProperty
        {
            get { return s_elemProp; }
        }

        private static void ValidateClassType(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            UDSSection section = (UDSSection)obj;
            if (!Enum.IsDefined(typeof(ClassType), section.ClassType))
            {
                throw new ConfigurationErrorsException(string.Format("Undefined class type: {0}.", section.ClassType));
            }
        }
    }
}
